﻿using System;
using System.Linq;
using System.Web.Mvc;
using GameSchool.Models;
using M = GameSchool.Models;
using GameSchool.Repository;
using GameSchool.Extra;
using GameSchool.Models.OnTheFlyModels;

namespace GameSchool.Controllers
{
	/// <summary>
	/// AssignmentController sér um alla virkni í sambandi við verkefni fyrir námskeið. 
	/// </summary> 

	[HandleError]
	[Authorize]
	public class AssignmentController : Controller
    {
        Repository.IGameSchoolRepository m_data = Repository.RepositoryMaster.CurrentRepository;
       
		// GET: /Assignments
        public ActionResult Index()
        {
			return View(m_data.Assignments.ToList());
        }
		
        // GET: /Assignment/Details/5
        public ActionResult Details(int id)
        {
            return View(m_data.Assignments.Find(id));
        }

        public ActionResult OpenAssignment(int id)
        { 
            //AssignmentInfoList assInfoList = new AssignmentInfoList();
            //assInfoList.Assignment = m_data.Assignments.Where(x => x.AssignmentId == id).FirstOrDefault();

            return View(m_data.Assignments.Where(x => x.AssignmentId == id).FirstOrDefault());
        }

		#region Assignments

		// GET: /Assignment/Create
		[Authorize (Roles="Teachers")]
        public ActionResult CreateAssignment(int id)
        {
            // id er id-ið á levelinu.
          //  ViewData[""] =                 // SAMA OG Í CREATE ASSIGNMENT, ÞURFUM EITTHVAÐ BETRA.
            return View();
        } 

        // POST: /Assignment/Create
        [HttpPost]
		[Authorize(Roles = "Teachers")]
        public ActionResult CreateAssignment(Assignment assignment)
        {
            //  LAGA --- Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

            Models.Person loggedOnPerson = UserInfo.GetLoggedOnPerson();
            if (loggedOnPerson != null)
            {

                assignment.EntryBy =  loggedOnPerson.PersonId;
                assignment.EntryDate = DateTime.Now;

                m_data.Assignments.Add(assignment);
                m_data.SaveChanges();
            }
            return RedirectToAction("Index");
            
            
            /*try
            {
				m_data.Assignments.Add(assignment);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }*/
        }
        
        // GET: /Assignment/Edit/5
		[Authorize(Roles = "Teachers")]
        public ActionResult EditAssignment(int id)
        {
            return View();
        }

        // POST: /Assignment/Edit/5
        [HttpPost]
		[Authorize(Roles = "Teachers")]
        public ActionResult EditAssignment(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

		#endregion

		#region Videos

		public ActionResult Video()
		{
			return View();
		}

		public ActionResult ShowAllVideos(int id)
		{
			return View(m_data.Assignments.Where(x=> x.AssignmentId == id).FirstOrDefault().VideoLectures);
		}

		// GET: /Assignment/Create
		[Authorize (Roles="Teachers")]
        public ActionResult CreateVideo()
        {
            return View();
        } 

        // POST: /Assignment/Create
        [HttpPost]
		[Authorize(Roles = "Teachers")]
        public ActionResult CreateVideo(VideoLecture videoLecture)
        {
            //Virkar
            try
            {
                if (ModelState.IsValid)
                {
                    Models.Person loggedOnPerson = UserInfo.GetLoggedOnPerson();
                    if (loggedOnPerson != null)
                    {
                        Assignment currentAssignment = m_data.Assignments.Where(x => x.AssignmentId == videoLecture.AssignmentId).SingleOrDefault();

                       
                            videoLecture.EntryById = loggedOnPerson.PersonId;

                            videoLecture.EntryDate = DateTime.Now;
                            currentAssignment.VideoLectures.Add(videoLecture);
                            m_data.SaveChanges();
                    }
                    return RedirectToAction("ShowAllVideos", new {id = videoLecture.AssignmentId});
                }
            }
            catch (Exception)
            {                
                ModelState.AddModelError("", "Það virkaði ekki að vista gögn ");
                throw;
            }

            return View();
        }
        
        
        // GET: /Assignment/Edit/5
		[Authorize(Roles = "Teachers")]
        public ActionResult EditVideo(int id)
        {
            var model = m_data.VideoLectures.Find(id);
            return View(model);
        }

        // POST: /Assignment/Edit/5
        [HttpPost]
		[Authorize(Roles = "Teachers")]
        public ActionResult EditVideo(int id, VideoLecture videoLecture)
        {
            try
            {
                var item = (from n in m_data.VideoLectures
                            where n.VideoLectureId == id
                            select n).SingleOrDefault();

                if (item != null)
                {
                    UpdateModel(item);                    
                    m_data.SaveChanges();
                    return RedirectToAction("ShowAllVideos", new {id = item.AssignmentId} );
                }
                else
                {
                    return RedirectToAction("Error");
                }

            }
             catch
                {
                     return View();
                }          
        }

        public ActionResult DetailsVideo(int id)
        {
            return View();
        }

       

		#endregion

	}
}
